//index.js
//获取应用实例
const app = getApp()
var config = require('../../utils/config.js')

Page({
  data: {
    motto: '医师合作商城',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })

    this.onLogin()
  },
  subscribeAction: function() {
    wx.navigateTo({
      url: '../subscribe/index'
    })
  },
  mySubscribeAction: function() {
    wx.navigateTo({
      url: '../mysubscribe/index'
    })
  },
  doctorSubscribeAction: function() {

    let group_id = wx.getStorageSync('group_id')
    if( group_id == 3) {
      wx.navigateTo({
        url: '../doctorsubscribe/index'
      })
    }else{
      wx.showToast({
        title: '您不是医师',
        icon: 'loading',
        duration: 1000
      })

      setTimeout(function () {
        wx.hideToast()
      }, 1000)
    }
  },
  gotoShop: function() {
    console.log( 'res' )

    wx.navigateToMiniProgram({
      appId: 'wx77b04271439dda0a',
      path: 'pages/index/index',
      extraData: {},
      envVersion: 'release',
      success(res) {
        // 打开成功
      },
      fail(res) {
        console.log( res )
      }
    })
  },
  onLogin: function() {
    var that = this
    wx.login({
      success: function(res) {
        if (res.code) {
          let nickname = ''
          if( that.data.userInfo ){
            // console.log( JSON.stringify(that.data.userInfo));
            nickname = that.data.userInfo.nickName
          }

          wx.request({
            url: config.siteURL + '/api/ucenter/wxLogin',
            data: {
              code: res.code,
              nickname: nickname
            },
            success: function(res) {

              // console.log( JSON.stringify(res) )

              if( res.data.code == 1) {
                wx.setStorage({
                  key: 'sessionKey',
                  data: res.data.data.sessionKey
                })
                wx.setStorage({
                  key: 'uid',
                  data: res.data.data.uid
                })

                wx.setStorage({
                  key: 'group_id',
                  data: res.data.data.group_id
                })
              }else{
                console.log('登录失败')
              }
            },
            fail: function(res) {},
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
})
